<?php

//礼券表
class ticket extends spModel
{
	public $pk = "id"; // 数据表的主键
    public $table = "tickets"; // 数据表的名称

   // 定义验证规则
	var $verifier = array(
		"rules" => array( // 规则
			'name'  => array('notnull' => TRUE),
			'price' => array('notnull' => TRUE),
			'value' => array('notnull' => TRUE),
			'intro' => array(
						'notnull' => TRUE, 
						'minlength' => 8,
						'maxlength' => 20
							)
						),
        "messages" => array( // 提示信息
            'name' => array('notnull' => "名称不能为空"),
            'price' => array('notnull' => "价格不能为空"),
            'value' => array('notnull' => "价值不能为空"),
            'intro' => array('notnull' => "介绍不能为空",
							 'minlength' => "介绍不能少于4个汉字"),
						)

	);
	
	function getAll($catid = null) {
		$sql = "SELECT g.*, gt.title FROM ticket t LEFT JOIN giftcat gt ON t.catid = gt.id";
		if ($catid) {
			$sql .= " WHERE t.catid = $catid";
		}
		$sql .= " ORDER BY value";
		$ret  = $this->findSql($sql);
		return $ret;
	}

	function create($row) {
		parent::create($row);
		$lastInsertId = $this->_db->newinsertid();
		if ($lastInsertId) {
			$mUserLog = spClass('userlog');
			$logArray = array(
								'uid' => $_SESSION['uid'],
								'type'=> 'addticket',
								'content' => 'Add Ticket ' . $lastInsertId . ':' . $row['name'],
								'dateline'=> time()
							);
		    $mUserLog->create($logArray);
		}
		return $lastInsertId;
	}
}